﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CLab.Common.View.Utils.Extensions;
using CLab.Base;
using System.Collections;
using CLab.PaqueteServiceReference;
using GandA.Component.Log;

namespace CLab.PagAdminLab
{
    public partial class CatPaquetes : PageBase
    {
        #region Eventos
        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 5;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Master.TituloPagina = "Paquetes de Estudios";
                LlenaCombos();
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetFocus(txtNom.ControlTextBox.ClientID);
            }
        }

        protected void BtnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                CargaGrid();
                if (grid.Rows.Count == 0)
                {
                    Master.alertSinDatos();
                }

                SetFocus(txtNom.ControlTextBox);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnExportar_Click(object sender, EventArgs e)
        {
            try
            {
                if (grid.Rows.Count > 0)
                {
                    grid.AllowPaging = false;
                    ////CargaGrid();
                    grid.ToExportCsv();
                    grid.AllowPaging = true;
                }
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnAgregar_Click(object sender, EventArgs e)
        {
            try
            {
                if (grid.Rows.Count > 0)
                {
                    SaveFilters();
                }

                Response.Redirect("CatPaqueteRegistro.aspx", false);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string[] sParams;
                switch (e.CommandName)
                {
                    case "cmdModificar":
                        sParams = e.CommandArgument.ToString().Split(',');
                        SaveFilters();
                        Response.Redirect(ResolveUrl("CatPaqueteRegistro.aspx?IdPaquete=" + sParams[0]), false);
                        break;
                    //case "cmdEliminar":
                    //    sParams = e.CommandArgument.ToString().Split(',');
                    //    EliminaPaquete(CLab.Common.Util.Convert.ToNInt(sParams[0]));
                    //    break;
                }
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grid.PageIndex = e.NewPageIndex;
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnElimina_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Common.Util.Convert.ToNInt(hdnId.Value).GetValueOrDefault(-1);
                EliminaPaquete(id);
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex, "Error en: btnElimina_Click");
                Master.alert(ex.Message);
            }
        }
        #endregion

        #region metodos
        private void LlenaCombos()
        {
            ///cmbEstudios.ObtenEstudios(SessionLaboratorio.IdLaboratorio, false);
        }

        private void CargaGrid()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();
            //Parametros
            parametros.Add(CreaParametroConsulta("@psDescripcion", txtNom.Text, ConsultaServiceReference.Parameter.vEnumType.VarChar));
            parametros.Add(CreaParametroConsulta("@pnIdLaboratorio", SessionLaboratorio.IdLaboratorio, ConsultaServiceReference.Parameter.vEnumType.Int));
            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);
            //Ejecuta conulta
            grid.DataSource = servicio.EjecutaConsultaConParametros("spObtenPaquetes", prms);
            grid.DataBind();
        }

        private void EliminaPaquete(int? idPaquete)
        {
            try
            {
                PaqueteServiceReference.PaqueteServiceClient servicio = new PaqueteServiceReference.PaqueteServiceClient();
                PaqueteServiceReference.Paquete obj = new PaqueteServiceReference.Paquete();
                obj.IdPaquete = idPaquete;
                obj.IdLaboratorio = SessionLaboratorio.IdLaboratorio;
                obj.IdUsuarioMod = SessionUsuario.IdUsuario;
                obj.IpMod = Request.UserHostAddress;
                servicio.EliminaPaquete(obj);
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }
        #endregion
    }
}